package com.project.controller;



import com.google.common.base.Enums;
import com.project.bean.InfoBean;
import com.project.bean.MQBean;
import com.project.bean.NewsEntity;
import com.project.bean.ShopBean;
import com.project.config.RedisCacheUtil;
import com.project.dao.NewsDao;
import com.project.service.IShopService;
import com.project.service.impl.RabbitMQService;
import com.project.service.impl.RabbitService;
import com.project.util.SnowIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("shop")
@Api(tags = "远程调用接口")
public class RedisController {
    @Autowired
    RedisCacheUtil redisCacheUtil;
    @Autowired
    IShopService service;
    @Autowired
    RabbitService rabbitService;
    @Autowired
    NewsDao dao;
//    @Autowired
//    private ISaleDao dao;
////    @RequestMapping("add")
////    public String add (ShopBean shopBean){
////        service.add(shopBean);
////        orderBean.setId(String.valueOf(System.currentTimeMillis()));
////        redisCacheUtil.hset(,orderBean.getId(),orderBean);
////        return "ok";
////    }
//
    @GetMapping("findAllByInfo/{info}")
    public List<NewsEntity> findAllByInfo(@PathVariable("info") String info){
        return dao.findAllByNewsInfo(info);
    }
    @RequestMapping("findAll")
    public List<Object> findAll(String userName){
        List<Object> list = new ArrayList<>();
        Set<Object> set= redisCacheUtil.hashGetFileds(userName);
        System.out.println(set);
        for (Object obj:set){
            list.add( redisCacheUtil.hget(userName, String.valueOf(obj)));
            System.out.println(obj);
        }
        return list;
    }
     @GetMapping("findInfo")
    public String findInfo(){
       String str  = String.valueOf(redisCacheUtil.get("info"));
       if (str.equals("1")){
           redisCacheUtil.del("info");
           return "ok";
       }
       return "no";
    }
//    @RequestMapping("update")
//    public String update( String key,String id, String name,Integer num,String movieName, Integer price, String date, String endDate){
//        OrderBean orderBean = new OrderBean(name,num,movieName, price,date,endDate);
//        orderBean.setId(id);
//        redisCacheUtil.hdel("order", String.valueOf(key));
//        redisCacheUtil.hset("order",key,orderBean);
//        return "ok";
//    }
//    @RequestMapping("del")
//    public String del(String key){
//        System.out.println(key);
//        redisCacheUtil.hdel("order",key);
//        return "ok";
//    }
    @ApiOperation("添加")
    @PostMapping ("addMQ")
    public String addMQ(@RequestBody InfoBean infoBean){
        System.out.println(infoBean);
        InfoBean infoBean1 = infoBean;
        infoBean1.setOrderNum(String.valueOf(System.currentTimeMillis()));
        rabbitService.sendUser(infoBean1);
        return "ok";
    }
    @GetMapping("findAllShop")
    public String findAllShop(){

        if (RabbitMQService.nums>0){
            return "ok";
        }
        return "no";
    }
}
